<?php

namespace app\api\controller;

use think\View;
use think\Controller;
use think\Db;
use \app\data\Data;
use app\api\model\News as news_list;
use think\Paginator;

class News extends Index
{
    /*
     * 政务工作新闻列表*/
    public function government_list()
    {
        $number = (isset($_GET['number']) && !empty($_GET['number'])) ? $_GET['number'] : 10;
        $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
        $search = (isset($_GET['search']) && $_GET['search']) ? trim($_GET['search']) : null;
        $type = (isset($_GET['type']) && $_GET['type']) ? trim($_GET['type']) : array('neq', 0);
        $status = (isset($_GET['status']) && $_GET['status']) ? trim($_GET['status']) : array('neq', 3);
        $order = (isset($_GET['order']) && $_GET['order']) ? trim($_GET['order']) : "time desc";
        $where['w.title'] = array('like', "%$search%");
        $where['w.status'] = $status;
        $where['w.type'] = $type;
        $alias = 'w';
        $news_list = new news_list('government_work');
        $count = $news_list->count($where, $alias);
        // dump($count);exit;
        //调用模型
        $condition = 'w.type=t.id';
        $data = $news_list->news_list('zgh_government_type', $where, $number, $page, $condition, $order);
        //分页
        $Page = $news_list->page($count, $number);
        $render = $Page->render;
        //html代码
        $res = model('Common')->page_data($count, $page, $number);
        $res['count'] = $count;
        $res['data'] = $data;
        $res['render'] = $render;
        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '新闻列表数据获取成功', $res);
        } else {
            $this->_jsonp('1000', '新闻列表数据获取失败', null);
        }
    }

    /*
     * 添加政务工作新闻*/
    public function government_add()
    {
        $news_list = new news_list('government_work');
        $res = $news_list->news_add();
        //$this->json('500', '新闻添加成功', $res);
        if ($res !== false) {
            action_log('新闻添加', 'log_admin',1);
            $this->json('200', '新闻添加成功', null);
        } else {
            action_log('新闻添加', 'log_admin');
            $this->json('1000', '新闻添加失败', null);
        }
    }

    /*
     * 政务工作新闻单条详情*/
    public function government_detail()
    {
        $id = input('get.id');
        $news_list = new news_list('government_work');
        $condition = "w.type=t.id";
        $res = $news_list->news_detail($id, 'zgh_government_type', $condition);
        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '新闻查询成功', $res);
        } else {
            $this->_jsonp('1000', '新闻查询失败', null);
        }
    }

    /*
     * 政务工作新闻单条修改*/
    public function government_edit()
    {
        $news_list = new news_list('government_work');
        $res = $news_list->news_edit();
        if ($res !== false) {
            action_log('新闻修改', 'log_admin',1);
            $this->json('200', '新闻修改成功', null);
        } else {
            action_log('新闻添加', 'log_admin');
            $this->json('1000', '新闻修改失败', null);
        }
    }

    /*
     * 政务工作新闻状态修改*/
    public function status_edit()
    {
        $true = auth();
        if ($true == 2) {
            $id = input('get.id');
            $data['status'] = input('get.status');
            $news_list = new news_list('government_work');
            $data = $news_list->news_del($id, $data);
            if ($data['res']) {
                action_log('新闻修改状态', 'log_admin',1);
                $this->_jsonp('200', $data['name'] . '成功', null);
            } else {
                action_log('新闻修改状态', 'log_admin');
                $this->_jsonp('1000', $data['name'] . '失败', null);
            }
        } else if($true == 1) {
            $this->_jsonp('1000', '您没有权限', null);
        }else{
			$this->_jsonp('1000','您未登录',null);
		}

    }

    /*
    * 平台资讯新闻类型*/
    public function government_type()
    {
        $where['status'] = 1;
        $res = Db::name('government_type')->where($where)->select();
        if (!empty($res) && isset($res)) {
            $news_list = new news_list();
            $data = $news_list->tree($res);
            $this->_jsonp('200', '新闻类型获取成功', $data);
        } else {
            $this->_jsonp('1000', '新闻类型获取失败', null);
        }
    }

    /*
     * 平台资讯新闻列表*/
    public function platform_list()
    {
        $number = 10;
        $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
        $search = (isset($_GET['search']) && $_GET['search']) ? trim($_GET['search']) : null;
        $type = (isset($_GET['type']) && $_GET['type']) ? trim($_GET['type']) : array('neq', 0);
        $where['w.title'] = array('like', "%$search%");
        $where['w.status'] = array('neq', 3);
        $where['w.type'] = $type;
        $alias = 'w';
        $news_list = new news_list('platform_information');
        $count = $news_list->count($where, $alias);
        //调用模型
        $condition = 'w.type=t.id';
        $data = $news_list->news_list('zgh_platform_type', $where, $number, $page, $condition);
        //dump($data);exit;
        $res = model('Common')->page_data($count, $page, $number);
        $res['count'] = $count;
        $res['data'] = $data;
        //dump($data);
        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '新闻列表数据获取成功', $res);
        } else {
            $this->_jsonp('1000', '新闻列表数据获取失败', null);
        }
    }

    /*
    * 平台资讯单条详情*/
    public function platform_detail()
    {
        $id = input('get.id');
        $news_list = new news_list('platform_information');
        $res = $news_list->news_detail($id);
        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '新闻查询成功', $res);
        } else {
            $this->_jsonp('1000', '新闻查询失败', null);
        }
    }

    /*
    * 添加平台资讯新闻*/
    public function platform_add()
    {
        $news_list = new news_list('platform_information');
        $res = $news_list->news_add();
        if ($res !== false) {
            $this->json('200', '新闻添加成功', null);
        } else {
            $this->json('1000', '新闻添加失败', null);
        }
    }

    /*
    * 修改平台资讯新闻*/
    public function platform_edit()
    {
        $news_list = new news_list('platform_information');
        $res = $news_list->news_edit();
        if ($res !== false) {
            $this->json('200', '新闻修改成功', null);
        } else {
            $this->json('1000', '新闻修改失败', null);
        }
    }

    /*
    * 平台资讯新闻状态改变*/
    public function platform_del()
    {
        $id = input('get.id');
        $data['status'] = input('get.status');
        $news_list = new news_list('platform_information');
        $data = $news_list->news_del($id, $data);
        if ($data['res']) {
            $this->_jsonp('200', $data['name'] . '成功', null);
        } else {
            $this->_jsonp('1000', $data['name'] . '失败', null);
        }
    }

    /*
    * 平台资讯新闻类型*/
    public function platform_type()
    {
        $where['status'] = 1;
        $res = Db::name('platform_type')->where($where)->select();
        if (!empty($res) && isset($res)) {
//            $news_list=new news_list();
//            $data=$news_list->tree($res);
            $this->_jsonp('200', '新闻类型获取成功', $res);
        } else {
            $this->_jsonp('1000', '新闻类型获取失败', null);
        }
    }

    /*
    * 前台新闻展示*/
    public function index()
    {
       $sql = "SELECT w.*,u.username,t.name FROM zgh_government_work w LEFT JOIN zgh_user u ON u.uid=w.uid LEFT JOIN zgh_government_type t ON w.type=t.id WHERE (SELECT count(*) FROM (SELECT * FROM zgh_government_work WHERE status=1)b WHERE b.type=w.type AND b.time>w.time)<2 AND w.status=1 AND w.type<>1 ORDER BY time desc";
        $res = Db::query($sql);
        $where['status']=1;
        $where['type']=1;
        $res1=Db::name('government_work')->where($where)->limit(4)->order('time desc')->select();
        $arr=array_merge_recursive($res1,$res);

//        $data=array(1,2,3,4,5,6,7);
//        $res=array();
//        foreach ($data as $k=>$v){
//            if($v==1){
//                $limit=4;
//            }else{
//                $limit=2;
//            }
//            $res[$v]=Db::name('government_work')->where($where)->limit($limit)->select();
//        }
        if (isset($arr) || !empty($arr)) {
            $this->_jsonp('200', '查询成功', $arr);
        } else {
            $this->_jsonp('1000', '查询失败', null);
        }
    }

    /*
     * 新闻的浏览
     * */
    public function views()
    {
        $where['id'] = input('get.id');
        $basename = input('get.sheet');
        $view = (isset($_GET['views']) && $_GET['views']) ? trim($_GET['views']) : "views";
        $news_list = new news_list($basename);
        $data = $news_list->plus($where, $view);
    }

    /*
     * 新闻的点赞
     * */
    public function likes()
    {
        $id = input('get.id');//新闻id
        $uid = input('get.uid');
        $where['mid']=$id;
        $where['static']=1;//表示点赞
        $where['uid']=$uid;
        $arr=Db::name('likes')->where($where)->find();
        if (!empty($arr)) {
            $data1['static']=2;
            $map1['id']=$arr['id'];
            $map2['id']=$id;
            $status = 0;
            Db::startTrans();
            try {
                Db::name('likes')->where($map1)->update($data1);
                Db::name('government_work')->where($map2)->setDec('likes');
                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
            }
            if ($status == 1) {
                $this->_jsonp('201', '取消点赞成功', null);
            } else {
                $this->_jsonp('1000', '取消点赞失败', null);
            }
        } else {
            $where['type']=2;
            $where['time']=time();
            $map2['id']=$id;
            $status = 0;
            Db::startTrans();
            try {
                Db::name('likes')->insert($where);
                Db::name('government_work')->where($map2)->setInc('likes');
                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
            }
            //讲点赞用户写入
            if ($status == 1) {
                $this->_jsonp('200', '点赞成功', null);
            } else {
                $this->_jsonp('1000', '点赞失败', null);
            }
        }
    }

    /*
     * 根据id查询的user收藏
     * */
    public function user_information()
    {
        $where['uid'] = input('get.id');
        $where['type'] = input('get.type');
        $where['static'] = 0;
        $collect = Db::name('collect')->where($where)->column('mid');
        if (!empty($collect) && $collect) {
            $this->_jsonp('200', '获取收藏成功', $collect);
        } else {
            $this->_jsonp('1000', '获取收藏失败', null);
        }
    }

    /*
    * 新闻收藏
    */
    public function collect()
    {
        $where['mid'] = input('get.id');//收藏的id
        $where['uid'] = input('get.uid');
        $where['type'] = 3;
        $where['static'] = 0;
        $base = new news_list('collect');
        $like = $base->check_name($where);
        $map2['id']=input('get.id');
        //dump($arr);
        if ($like) {
            $map1['id']=$like['id'];
            $data['static']=1;
            $like_add = Db::name("collect")->where($map1)->update($data);
            $add = Db::name("government_work")->where($map2)->setDec('collect');
            if ($like_add !== false) {
                $this->_jsonp('200', '取消收藏成功', null);
            } else {
                $this->_jsonp('1000', '取消收藏失败', null);
            }
        } else {
            $data1['uid']=input('get.uid');
            $data1['time']=time();
            $data1['type']=3;
            $data1['mid']=input('get.id');
            // dump($data);die;
            //收藏用户写入
            $like_add = Db::name("collect")->insert($data1);
            $add = Db::name("government_work")->where($map2)->setInc('collect');
            if ($like_add !== false) {
                $this->_jsonp('201', '新闻收藏成功', null);
            } else {
                $this->_jsonp('1000', '新闻收藏失败', null);
            }
        }
    }

    /*
     * 知产企业
     * */
    public function company()
    {
        $lot = (isset($_GET['num']) && !empty($_GET['num'])) ? $_GET['num'] : 1;
        $where['lot'] = $lot;
        $where['status'] = 1;
        $news_list = new news_list('company');
        $data = $news_list->table_list($where, 'name');
        if (!empty($data)) {
            $this->_jsonp('200', '查询成功', $data);
        } else {
            $this->_jsonp('1000', '查询失败', null);
        }
    }

    /*
     * 随机5个知产企业
     * */
    public function info()
    {
        $news = new news_list('company');
        $where['status'] = 1;
        $limit = 5;
        $data = $news->rand_list($where, "name", $limit);
        if (!empty($data)) {
            $this->_jsonp('200', '查询成功', $data);
        } else {
            $this->_jsonp('1000', '查询失败', null);
        }
    }
	/*
     * 新知产企业
     * */
    public function new_company()
    {
        $type = (isset($_GET['type']) && !empty($_GET['type'])) ? $_GET['type'] : array('neq' ,0);
        $where['status'] = 1;
		$where['type'] = $type;
		$limit = (isset($_GET['number']) && !empty($_GET['number'])) ? $_GET['number'] : 14;
        $data = Db::name('companys')->where($where)->field('id,name')->limit($limit)->order('rand()')->select();
		
        if (!empty($data)) {
            $this->_jsonp('200', '查询成功', $data);
        } else {
            $this->_jsonp('1000', '查询失败', null);
        }
    }
	 /*
     *新随机知产信息
     * */
    public function new_info(){
       
        $where['status']=2;
        $limit = (isset($_GET['number']) && !empty($_GET['number'])) ? $_GET['number'] : 15;
        $data= Db::name('policy')->field('id,content')
            ->where($where)->order('rand()')
            ->limit($limit)
            ->select();
        if(!empty($data)){
            $this->_jsonp('200','查询成功',$data);
        }else{
            $this->_jsonp('1000','查询失败',null);
        }
    }
	/*
     * 新资讯信息
     * */
    public function new_government(){
       
        $type = (isset($_GET['type']) && !empty($_GET['type'])) ? $_GET['type'] : array('neq' ,0);
        $limit = (isset($_GET['number']) && !empty($_GET['number'])) ? $_GET['number'] : 10;
		$where['type']=$type;
		$where['status']=1;
        $data= Db::name('government_work')->field('id,type,title,abstract,pic')
            ->where($where)->order('time desc')
            ->limit($limit)
            ->select();
        if(!empty($data)){
            $this->_jsonp('200','查询成功',$data);
        }else{
            $this->_jsonp('1000','查询失败',null);
        }
    }
	/*
     * 新资推荐
     * */
    public function governments(){
        $limit = (isset($_GET['number']) && !empty($_GET['number'])) ? $_GET['number'] : 6;	
		$where['w.status']=1;
        $data= Db::name('government_work')->alias('w')
			->join('zgh_government_type t','t.id=w.type')
			->field('w.id,t.name,w.title,w.abstract')
            ->where($where)->order('w.time desc,w.views desc')
            ->limit($limit)
            ->select();
        if(!empty($data)){
            $this->_jsonp('200','查询成功',$data);
        }else{
            $this->_jsonp('1000','查询失败',null);
        }
    }
	/*
     * 新资讯banner
     * */
    public function new_banner(){
        $limit = (isset($_GET['number']) && !empty($_GET['number'])) ? $_GET['number'] : 8;	
		$where['status']=1;
        $data= Db::name('banner_sheet')
			->field('title,img_url,url')
            ->where($where)->order('time desc')
            ->limit($limit)
            ->select();
        if(!empty($data)){
            $this->_jsonp('200','查询成功',$data);
        }else{
            $this->_jsonp('1000','查询失败',null);
        }
    }
}